package wangjg.coco.actions;


import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

import wangjg.coco.views.Model2SourceDialog;

/**
 * @author wangjigang1
 *
 */
public class Model2SourceAction implements IObjectActionDelegate {
	private Shell shell;
	
 	/**
	 * Constructor for Action1.
	 */
	public Model2SourceAction() {
		super();
	}
 
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		this.shell = targetPart.getSite().getShell();
	}
 
	public void run(IAction action) {
		Object element = selection.getFirstElement();
		if(element instanceof IFile || element instanceof IFolder){
			IResource resource = ((IResource)element);
			String path = resource.getLocation().toFile().getPath();
			
			Model2SourceDialog dialog = new Model2SourceDialog(new Shell());
			dialog.setSelectedPath(path);
			int result = dialog.open();
			if(result==0){
				try {
					resource.refreshLocal(IResource.DEPTH_ONE, null);
				} catch (CoreException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	private IStructuredSelection selection;
 
	public void selectionChanged(IAction action, ISelection selection) {
		if(selection instanceof  IStructuredSelection){
			this.selection = (IStructuredSelection) selection;
		}
	} 
 
}